package com.loginlib.table.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loginlib.R;
import com.loginlib.table.TableBillActivity;
import com.loginlib.table.TableNumActivity;
import com.wanhe.base.model.base.Table;
import com.wanhe.base.model.holder.TableInfoHolder;
import com.wanhe.base.util.TimeUtil;


/**
 * Created by Administrator on 2016/7/26.
 */
public class TableItem extends RelativeLayout {
    TextView tvTableName;
    TextView tvIsVIP;
    TextView tvOpenTableTime;
    TextView tvCustomNumber;
    TextView tvTotalPrice;
    RelativeLayout rlBottomLayout;
    private Table mTable;

    private boolean isOpened = false;


    public TableItem(Context context) {
        this(context, null);
    }

    public TableItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.table_item_layout, this, true);
        initId();
        initEvent();
    }

    private void initId(){
        tvTableName = (TextView) findViewById(R.id.tv_table_name);
        tvIsVIP = (TextView) findViewById(R.id.tv_isVIP);
        tvOpenTableTime = (TextView) findViewById(R.id.tv_open_table_time);
        tvCustomNumber = (TextView) findViewById(R.id.tv_custom_number);
        tvTotalPrice = (TextView) findViewById(R.id.tv_total_price);
        rlBottomLayout = (RelativeLayout) findViewById(R.id.rl_bottom_layout);
    }


    //人数界面
    private void jumpTableNum() {
        TableInfoHolder.getInstance().setTableName(mTable.getName());
        TableInfoHolder.getInstance().setTableNum(mTable.getID());

        Intent intent = new Intent(getContext(), TableNumActivity.class);
        intent.putExtra(TableNumActivity.TABLE_NAME, mTable.getID());
        intent.putExtra(TableNumActivity.TABLE_BILLAMOUT, mTable.getBillAmount());
        intent.putParcelableArrayListExtra(TableNumActivity.TABLE_DATA, mTable.getStates());
        getContext().startActivity(intent);

    }

    //已选菜品界面
    private void jumpTableBill() {
        TableInfoHolder.getInstance().setTableName(mTable.getName());
        TableInfoHolder.getInstance().setTableNum(mTable.getID());

        Intent intent = new Intent(getContext(), TableBillActivity.class);
        intent.putExtra(TableBillActivity.TABLEID, mTable.getID());//桌号
        intent.putExtra(TableBillActivity.TABLE_BILLAMOUNT, mTable.getBillAmount());//消费总额
        intent.putParcelableArrayListExtra(TableBillActivity.TABLESTATE, mTable.getStates());
        getContext().startActivity(intent);
    }

//    /**
//     * 跳转到脆鱼账单界面
//     */
//    private void toCuiYuBill() {
//        TableInfoHolder.getInstance().setTableName(mTable.getName());
//        TableInfoHolder.getInstance().setTableNum(mTable.getID());
//
//        Intent intent = new Intent(getContext(), CuiYuBillActivity.class);
//        intent.putExtra(CuiYuBillActivity.TABLEID, mTable.getID());//桌号
//        intent.putExtra(CuiYuBillActivity.TABLE_BILLAMOUNT, mTable.getBillAmount());//消费总额
//        getContext().startActivity(intent);
//    }

    /**
     * 长按显示选人数，点击直接进入cai
     */
    private void initEvent() {
        this.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                jumpTableNum();
                return false;
            }
        });

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mTable.getTableOpened() == Table.EMPTY) {
                    jumpTableNum();
                } else if (mTable.getTableOpened() == Table.OPEN) {
                    //获取默认的开台人数
                    TableInfoHolder.getInstance().setCustomNum(mTable.getTablePpl());
//                    jumpTableBill();
//                    toCuiYuBill();
                } else if (mTable.getTableOpened() == Table.ERROR) {
                    return;
                }
            }
        });
    }


    public void setValue(Table table) {
        this.mTable = table;
        tvTableName.setText(table.getName());
        // TODO: 2016/7/26 餐桌消费价格
        int isOpen = table.getTableOpened();
        switch (isOpen) {
            // TODO: 2016/7/26 设置背景颜色
            case Table.EMPTY:
                rlBottomLayout.setVisibility(GONE);
                setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.table_bg_empty_gray));
                break;
            case Table.ERROR:
                // TODO: 2016/7/26 错误颜色
                break;
            case Table.OPEN:
                isOpened = true;
                rlBottomLayout.setVisibility(VISIBLE);
                setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.table_bg_normal_green));
                tvCustomNumber.setText(table.getTablePpl() + "人");

                int time = Integer.valueOf(table.getTableOpenTime());
                String timer = TimeUtil.secToTime(time);
                tvOpenTableTime.setText(timer);
                tvTotalPrice.setText(table.getBillAmount() + "元");//设置总消费
                break;
        }
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }


}
